package org.link.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.link.bean.Order;
import org.link.bean.ProCollection;
import org.link.bean.Product;
import org.link.dao.ProductDaoImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
/**
 * 
 * project_name:springdemo2
 * package_name:org.link.web
 * file_name:AnnotationTest.java
 * @author Link
 * create_date:2015年11月15日上午11:31:56
 * modified_date:
 * description: spring注解模式servlet
 *
 */
/** 
 *使用@Controller注解就表示此类是控制器
 */
@Controller
public class AnnotationTest {
	ProductDaoImpl productDaoImpl = new ProductDaoImpl();
	private static SerializeConfig mapping = new SerializeConfig();  //自定义fastJSON的日期格式
	private static String dateFormat;  
	static {  
	    dateFormat = "yyyy-MM-dd";  
	    mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));  
	} 
	
	//使用@RequestMapping就代表请求的路径
	@RequestMapping("/hello1")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("utf-8");
		ModelAndView modelAndView = new ModelAndView();
		ProCollection proCollection = new ProCollection();
		Order order1 = new Order("p_count","desc");
		int begin = 0;//页面起始位置
		int rows = 5;//每页行数
		int totalPage = 0;//设置总共页数
		List<Order> orders = new ArrayList<Order>();
		orders.add(order1);
		proCollection.setOrders(orders);
		proCollection.setBegin(begin);
		proCollection.setRows(rows);
		
		ArrayList<String> allBrand = (ArrayList<String>) productDaoImpl.selectAllBrand();//查询并返回所有品牌集合
		ArrayList<String> allNetwork = (ArrayList<String>) productDaoImpl.selectAllNetwork();//查询并返回所以网络类型集合
		ArrayList<Double> allSize = (ArrayList<Double>) productDaoImpl.selectAllSize();//查询并返回所有尺寸大小集合
		ArrayList<String> allRom = (ArrayList<String>) productDaoImpl.selectAllRom();//查询并返回所有内存大小集合
		
		ArrayList<Product> products = (ArrayList<Product>) productDaoImpl.selectProductByProCollection(proCollection);
		int totalCount = productDaoImpl.selectProductCount();
		if(totalCount%rows==0){
			totalPage = totalCount/rows;
		}else{
			totalPage = totalCount/rows+1;
		}
		modelAndView.addObject("allProduct", products);
		modelAndView.addObject("curPage", 1);//设置当前页数
		modelAndView.addObject("totalPage", totalPage);//设置总共页数
		modelAndView.addObject("allBrand", allBrand);
		modelAndView.addObject("allNetwork", allNetwork);
		modelAndView.addObject("allSize", allSize);
		modelAndView.addObject("allRom", allRom);
		modelAndView.setViewName("index");
		//response.sendRedirect("/WEB-INF/jsp/index.jsp");
		return modelAndView;
	}
	
	/**
	 * 
	 * description: 多条件查询以及页面切换通过数据库查询并返回的数据
	 * 2015年11月18日 上午10:11:43
	 * @author Link
	 * @param proCollection
	 *
	 */
	@ResponseBody
	@RequestMapping("pageChange")
	public ArrayList<Product> pageChange(@RequestBody ProCollection proCollection,HttpServletResponse response){
		ArrayList<Product> products = (ArrayList<Product>) productDaoImpl.selectProductByProCollection(proCollection);
		if(products.size()!=0){
			proCollection.setBegin(null);
			proCollection.setRows(null);
			int totalCount = productDaoImpl.selectProductCountByProCollection(proCollection);
			int totalPage = 0;
			if(totalCount%5 == 0){
				totalPage = totalCount/5;
			}else{
				totalPage = totalCount/5+1;
			}
			products.get(0).setTotalPage(totalPage);
		}
		return products;
	}
}
















